package com.extension.company.common.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.extension.company.admin.R;
import com.extension.company.common.utils.StringUtils;


/**
 * dialog弹窗
 */
public class DialogOK extends Dialog implements OnClickListener {
	private TextView tv_title, tv_content;
	private TextView btn_cancel, btn_ok;
	private DialogOKListener listener;

	public DialogOK(Context context) {
		super(context, R.style.dialog_alert);
		LayoutInflater inflater = getLayoutInflater();
		View view = inflater.inflate(R.layout.dialog_ok, null);
		setContentView(view);
		//初始化控件
		initView(view);
		//设置监听器
		setListener();
	}

	public DialogOK(Context context, DialogOKListener listener) {
		super(context, R.style.dialog_alert);
		this.listener = listener;
		LayoutInflater inflater = getLayoutInflater();
		View view = inflater.inflate(R.layout.dialog_ok, null);
		setContentView(view);
		//初始化控件
		initView(view);
		//设置监听器
		setListener();
	}

	public DialogOK(Context context, String title, String content, DialogOKListener listener) {
		super(context, R.style.dialog_alert);
		this.listener = listener;
		LayoutInflater inflater = getLayoutInflater();
		View view = inflater.inflate(R.layout.dialog_ok, null);
		setContentView(view);
		initView(view, title, content);
		setListener();
	}

	//监听器
	private void setListener() {
		btn_cancel.setOnClickListener(this);
		btn_ok.setOnClickListener(this);
	}

	//初始化控件
	private void initView(View view, String title, String content) {
		tv_title = view.findViewById(R.id.tv_title);
		tv_content = view.findViewById(R.id.tv_content);
		btn_cancel = view.findViewById(R.id.btn_cancel);
		btn_ok = view.findViewById(R.id.btn_ok);
		//设置标题和内容
		tv_title.setText(title);
		tv_content.setText(content);
		setCanceledOnTouchOutside(false);
	}

	private void initView(View view) {
		tv_title = view.findViewById(R.id.tv_title);
		tv_content = view.findViewById(R.id.tv_content);
		btn_cancel = view.findViewById(R.id.btn_cancel);
		btn_ok = view.findViewById(R.id.btn_ok);
		setCanceledOnTouchOutside(false);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.btn_ok:
				if (listener != null) {
					listener.dialogOKListenerOK();
				}
				dismiss();
				break;
			case R.id.btn_cancel:
				if (listener != null) {
					listener.dialogOKListenerCancel();
				}
				dismiss();
				break;
			default:
				break;
		}
	}

	//设置标题
	public void setTitle(String title) {
		tv_title.setText(title);
	}

	//设置内容
	public void setContent(String content) {
		tv_content.setText(content);
	}

	//获取内容
	public String getContent() {
		return StringUtils.getText(tv_content);
	}

	public void setButtonName(String cancel, String ok) {
		btn_cancel.setText(cancel);
		btn_ok.setText(ok);
	}

	public void setDialogOKListener(DialogOKListener listener) {
		this.listener = listener;
	}

	public interface DialogOKListener {
		/**
		 * true确定, false取消
		 */
		void dialogOKListenerOK();

		/**
		 * 关闭
		 */
		void dialogOKListenerCancel();
	}

	/**
	 * 物理按键返回
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			if (listener != null) {
				listener.dialogOKListenerCancel();
			}
		}
		return super.onKeyDown(keyCode, event);
	}
}





